#include "AI.h"

extern map<string, string> type_map;
extern map<string, string>::iterator type_it;

bool CheckTag(SynsetPtr ss, char* type);

bool WordnetTag(char* word, char* type)
{
	SynsetPtr synset_INSTANCE = NULL;
	SynsetPtr synset_NEXT = NULL;
	SynsetPtr synset_PTRLIST = NULL;
	SynsetPtr synset_TRACE = NULL;
	SynsetPtr synset_TMP = NULL;

	synset_INSTANCE = findtheinfo_ds(word, NOUN, INSTANCE, ALLSENSES);

	synset_NEXT = synset_INSTANCE;

	while(synset_NEXT != NULL)
	{
		synset_PTRLIST = synset_NEXT;

		while(synset_PTRLIST != NULL)
		{
			if(CheckTag(synset_PTRLIST, type) == true)
			{
				if(synset_INSTANCE != NULL)
					free_syns(synset_INSTANCE);
				if(synset_TRACE != NULL)
					free_syns(synset_TRACE);
				return true;
			}

			synset_TMP = synset_PTRLIST;
			synset_PTRLIST = synset_PTRLIST->ptrlist;
		}

		synset_TRACE = traceptrs_ds(synset_TMP, HYPERPTR, NOUN, 1);
		synset_PTRLIST = synset_TRACE;

		while(synset_PTRLIST != NULL)
		{
			if(CheckTag(synset_PTRLIST, type) == true)
			{
				if(synset_INSTANCE != NULL)
					free_syns(synset_INSTANCE);
				if(synset_TRACE != NULL)
					free_syns(synset_TRACE);
				return true;
			}

			synset_PTRLIST = synset_PTRLIST->ptrlist;
		}

		synset_NEXT = synset_NEXT->nextss;
	}

	if(synset_INSTANCE != NULL)
		free_syns(synset_INSTANCE);
	if(synset_TRACE != NULL)
		free_syns(synset_TRACE);
	return false;
}

bool CheckTag(SynsetPtr ss, char* type)
{
	for(int i = 0; i < ss->wcount; i++)
	{
		type_it = type_map.find(ss->words[i]);

		if(type_it != type_map.end())
		{
			strcpy_s(type, TYPE_LENGTH, type_it->second.c_str());
			return true;
		}
	}

	return false;
}